package com.system.connect.springboot_flowers.utils;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 阿里巴巴百炼模型测试类
 */
@Slf4j
public class DashScopeTest {
    
    private static final String API_KEY = "sk-df225849bbc74813b7e4ef604ae7ed1f";
    
    /**
     * 测试百炼模型对话
     */
    public static void testChat() {
        try {
            Generation gen = new Generation();
            Message systemMsg = Message.builder()
                    .role(Role.SYSTEM.getValue())
                    .content("你是一个智能助手")
                    .build();
            Message userMsg = Message.builder()
                    .role(Role.USER.getValue())
                    .content("请介绍你自己")
                    .build();
            
            QwenParam param = QwenParam.builder()
                    .model(Generation.Models.QWEN_TURBO)
                    .messages(Arrays.asList(systemMsg, userMsg))
                    .resultFormat(QwenParam.ResultFormat.MESSAGE)
                    .apiKey(API_KEY)
                    .build();
            
            String response = gen.call(param).getOutput().getChoices().get(0).getMessage().getContent();
            log.info("百炼模型回复：{}", response);
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            log.error("调用百炼模型失败：{}", e.getMessage());
            log.error("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        }
    }
    
    /**
     * 测试百炼模型对话（带自定义提示词）
     *
     * @param prompt 用户输入的提示词
     */
    public static String chat(String prompt) {
        try {
            Generation gen = new Generation();
            Message systemMsg = Message.builder()
                    .role(Role.SYSTEM.getValue())
                    .content("你是一个智能助手")
                    .build();
            Message userMsg = Message.builder()
                    .role(Role.USER.getValue())
                    .content(prompt)
                    .build();
            
            QwenParam param = QwenParam.builder()
                    .model(Generation.Models.QWEN_TURBO)
                    .messages(Arrays.asList(systemMsg, userMsg))
                    .resultFormat(QwenParam.ResultFormat.MESSAGE)
                    .apiKey(API_KEY)
                    .build();
            
            return gen.call(param).getOutput().getChoices().get(0).getMessage().getContent();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            log.error("调用百炼模型失败：{}", e.getMessage());
            return "抱歉，我现在无法回答，请稍后再试。";
        }
    }

    public static void main(String[] args) {
        // 测试基本对话
        testChat();
        
        // 测试自定义提示词
        String response = chat("请介绍一下你自己");
        log.info("自定义提示词回复：{}", response);
    }
}